/* Exercise 3 in chpater two of the Tronixstuff blog. This 
   program will take the temperature reading of an TMP35 sensor
   and display the values on an LCD screen in both Celcius and
   Farenheit. Using two buttons, the minimum and maximum
   temperatures will also be displayed on the LCD with the option
   to reset the max/min as desired. 
   
   Psuedocode
   read in temperature value and convert to proper units
   compare temp values to min/max and store if necessary
   check button states
   if button 1 pressed, show max/min, else, leave as current temp
   if button 2 pressed, ask if reset wanted, reset max/min values
*/
   
#include <LiquidCrystal.h>

// Initializations
int maxMinButton = 8;
int resetButton = 9;
float maxTempF = -999.0;
float minTempF = 999.0;
float maxTempC = -999.0;
float minTempC = 999.0;
float currentTempF = 0.0;
float currentTempC = 0.0;
float a2dVal = 0.0;
float A2DCONVERTMV = 5000.0/1024;


LiquidCrystal lcd(2,3,4,5,6,7);

void setup() {
   // Start LCD code for a 2 line, 16 character LCD
   lcd.begin(16,2); // Column x row notation like C, not MATLAB
   lcd.clear(); // Clear anything that may be on the screen
   lcd.print("Temp Program");
   lcd.setCursor(0,1);
   lcd.print("By Kenta Hood");
   delay(5000);
   
   pinMode(maxMinButton,INPUT);
   pinMode(resetButton,INPUT);
}

void loop() {
   lcd.clear();

   // read in temperatures, convert, store and display
   a2dVal = analogRead(5);
   currentTempC = a2dVal * A2DCONVERTMV / 10;
   currentTempF = currentTempC * (9.0/5.0) + 32;
   
   if (currentTempF > maxTempF) {
      maxTempC = currentTempC;
      maxTempF = currentTempF;
   } else if (currentTempF < minTempF) {
      minTempC = currentTempC;
      minTempF = currentTempF;
   }
   
   // print current temp in C and F
   lcd.print("Current Temp:");
   lcd.setCursor(0,1);
   lcd.print(currentTempC,1);
   lcd.print(" C/");
   lcd.print(currentTempF,1);
   lcd.print(" F");
   
   // max/min display logic
   if (digitalRead(maxMinButton) == LOW) {
      // display maximum temperature
      lcd.clear();
      lcd.print("Maximum Temp:");
      lcd.setCursor(0,1);
      lcd.print(maxTempC,1);
      lcd.print(" C/");
      lcd.print(maxTempF,1);
      lcd.print(" F");
      delay(3000);

      // display minimum temperature
      lcd.clear();
      lcd.print("Minimum Temp:");
      lcd.setCursor(0,1);
      lcd.print(minTempC,1);
      lcd.print(" C/");
      lcd.print(minTempF,1);
      lcd.print(" F");
      delay(3000);
   }
   
   // max/min reset logic
   if (digitalRead(resetButton) == LOW) {
      lcd.clear();
      lcd.print("Reset Max/Min?");
      lcd.setCursor(0,1);
      lcd.print("Yes: 1  No: 0");
      delay(500); // delay 0.5 seconds after button push
      
      // wait for user input
      while ((digitalRead(resetButton) && digitalRead(maxMinButton)) != LOW) {}
      
      if (digitalRead(resetButton)) {
         maxTempF = -999.0;
         maxTempC = -999.0;
         minTempF = 999.0;
         minTempC = 999.0;
         lcd.clear();
         lcd.print("Max/Min Reset!");
         delay(500);
      }
   }
   
   delay(1000); // update temperatures every second
}
